<?php


class Model_Roles extends Zend_Db_Table_Abstract  {

    protected $_name = 'tbl_administracion_roles';

    public function GetTblName() {
        return $this->_name;
    }
    
    public function GetRolById($rolId) {
        $db = Zend_Db_Table::getDefaultAdapter();
        $select = $this->select();
        $select->from(array($this->_name));
        $select->where('roles_id = "'.$rolId.'"');
        $select->setIntegrityCheck(false);
        $query = $db->query($select);
        return $query->fetchAll();
    }
    
    public function GetRolNameExist($rolName) {
        $db = Zend_Db_Table::getDefaultAdapter();
        $select = $this->select();
        $select->from(array($this->_name));
        $select->where('roles_nombre = "'.$rolName.'"');
        $select->setIntegrityCheck(false);
        $query = $db->query($select);
        return (count($query->fetchAll()) > 0);
    }
    
    public function GetRoles($like = null){
        $db = Zend_Db_Table::getDefaultAdapter();
        $select = $this->select();
        $select->from(array($this->_name));
        if ($like != null) {
            $select->where('`roles_nombre` LIKE "%'.$like.'%"');
        }
        $select->order('roles_nombre');
        $select->setIntegrityCheck(false);
        $query = $db->query($select);
        return $query->fetchAll();        
    }

    public function Guardar($rol) {
        $arr_rol = $rol->ToArray();
        
        $existe = $this->GetRolNameExist($arr_rol['roles_nombre']);
        if (!$existe) {
            $db = Zend_Db_Table::getDefaultAdapter();
            $query = "INSERT INTO `".$this->_name;

            $query .= "` (";
            foreach ($arr_rol as $attr => $val ) {
                $query .= "`".$attr."`, ";
            }
            $query = substr($query, 0, -2);
            $query .= ") VALUES (";
            foreach ($arr_rol as $attr => $val ) {
                $query .= "'".$val."', ";
            }
            $query = substr($query, 0, -2);
            $query .= ");";
            
            $db->query($query);
            return true;
        } else {
            return false;
        }
    }
    
    public function Modificar($rolid, $rol) {
        $arr_rol = $rol->ToArray();
        
        $existe = $this->GetRolNameExist($arr_rol['roles_nombre']);
        $oldrol = $this->GetRolById($rolid);
        if (!$existe || ($existe && $oldrol[0]['roles_nombre'] == $rol->nombre)) {
            $db = Zend_Db_Table::getDefaultAdapter();
            
            $query = "UPDATE `".$this->_name."` SET ";
            
            foreach ($arr_rol as $attr => $val ) {
                if ($attr != 'roles_id') {
                    $query .= "`".$attr."` = '".$val."', ";
                }
            }
            $query = substr($query, 0, -2);
            $query .= " WHERE `roles_id` = '".$rolid."';";
            
            $db->query($query);
            return true;
        } else {
            return false;
        }
    }
}
